home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / rot.pro < prev    next >
Text File  |  1997-07-08  |  5KB  |  147 lines

  1. ; $Id: rot.pro,v 1.5 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1982-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5.  
  6. ;Rotate and magnify an image
  7. FUNCTION ROT,A,ANGLE,MAG,X0,y0, INTERP = interp, MISSING = missing, $
  8.         PIVOT = pivot, CUBIC = cubic
  9. ;+
  10. ; NAME:
  11. ;    ROT
  12. ;
  13. ; PURPOSE:
  14. ;    Rotate, magnify or demagnify, and/or translate an image.
  15. ;
  16. ; CATEGORY:
  17. ;    Z3 - Image processing, geometric transforms.
  18. ;
  19. ; CALLING SEQUENCE:
  20. ;    Result = ROT(A, Angle, [Mag, X0, Y0], MISSING = missing,
  21. ;        INTERP = Interp, CUBIC = Cubic)
  22. ;
  23. ; INPUTS:
  24. ;    A:    The image array to be rotated.  This array may be of any type,
  25. ;        but it must have two dimensions.
  26. ;
  27. ;    ANGLE:    Angle of rotation in degrees CLOCKWISE. (Why?,
  28. ;        because of an error in the old ROT.)
  29. ;
  30. ; OPTIONAL INPUT PARAMETERS:
  31. ;    MAG:    Magnification/demagnification factor.  A value of 1.0 = no
  32. ;        change, > 1 is magnification and < 1 is demagnification.
  33. ;
  34. ;    X0:    X subscript for the center of rotation.  If omitted, X0 equals
  35. ;        the number of columns in the image divided by 2.
  36. ;
  37. ;    Y0:    Y subscript for the center of rotation.  If omitted, y0 equals
  38. ;        the number of rows in the image divided by 2.
  39. ;
  40. ; KEYWORDS:
  41. ;    INTERP:    Set this keyword for bilinear interpolation.  If this keyword
  42. ;        is set to 0 or omitted, nearest neighbor sampling is used.
  43. ;        Note that setting this keyword is the same as using the 
  44. ;        ROT_INT User Library function.  This change (and others) 
  45. ;        essentially makes ROT_INT obsolete.
  46. ;
  47. ;    CUBIC:    If specified and non-zero, "Cubic convolution"
  48. ;        interpolation is used.  This is a more
  49. ;        accurate, but more time-consuming, form of interpolation.
  50. ;        CUBIC has no effect when used with 3 dimensional arrays.
  51. ;        If this parameter is negative and non-zero, it specifies the
  52. ;        value of the cubic interpolation parameter as described
  53. ;        in the INTERPOLATE function.  Valid ranges are -1 <= Cubic < 0.
  54. ;        Positive non-zero values of CUBIC (e.g. specifying /CUBIC)
  55. ;        produce the default value of the interpolation parameter
  56. ;        which is -1.0.
  57. ;
  58. ;      MISSING:    The data value to substitute for pixels in the output image 
  59. ;        that map outside the input image.
  60. ;
  61. ;      PIVOT: Setting this keyword causes the image to pivot around the point
  62. ;        X0, Y0, so that this point maps into the same point in the
  63. ;        output image.  If this keyword is set to 0 or omitted, then the
  64. ;        point X0, Y0 in the input image is mapped into the center of
  65. ;        the output image.
  66. ;
  67. ; OUTPUTS:
  68. ;    ROT returns a rotated, magnified, and translated version of the
  69. ;    input image.  Note that the dimensions of the output image are
  70. ;    always the same as those of the input image.
  71. ;
  72. ; COMMON BLOCKS:
  73. ;    None.
  74. ;
  75. ; SIDE EFFECTS:
  76. ;    None.
  77. ;
  78. ; RESTRICTIONS:
  79. ;    None.
  80. ;
  81. ; PROCEDURE:
  82. ;    The POLY_2D function is used to translate, scale, and
  83. ;    rotate the original image.
  84. ;
  85. ; EXAMPLE:
  86. ;    Create and display an image.  Then display a rotated and magnified
  87. ;    version.  Create and display the image by entering:
  88. ;
  89. ;        A = BYTSCL(DIST(256))
  90. ;        TV, A
  91. ;
  92. ;    Rotate the image 33 degrees and magnify it 1.5 times.  Use bilinear
  93. ;    interpolation to make the image look nice.  Enter:
  94. ;
  95. ;        B = ROT(A, 33, 1.5, /INTERP)
  96. ;        TV, B
  97. ;    
  98. ; MODIFICATION HISTORY:
  99. ;    June, 1982.     Written by DMS, RSI.
  100. ;
  101. ;    Feb, 1986.     Modified by Mike Snyder, ES&T Labs, 3M Company.
  102. ;             Adjusted things so that rotation is exactly on the 
  103. ;            designated center.
  104. ;
  105. ;    October, 1986.  Modified by DMS to use the POLY_2D function.
  106. ;
  107. ;    Aug, 1988.    Added INTERP keyword.
  108. ;       Dec, 1992.      Added PIVOT keyword, William Thompson, NASA/GSFC.
  109. ;    Nov, 1993.    Added CUBIC keyword, DMS/RSI.
  110. ;-
  111. ;
  112. ;
  113. on_error,2        ;Return to caller if error
  114. B = FLOAT(SIZE(A))    ;Get dimensions
  115. IF N_PARAMS(0) LT 5 THEN BEGIN
  116.     X0 = (B[1]-1)/2.        ;Center of rotation in X.
  117.     Y0 = (B[2]-1)/2.        ; and in Y.
  118.     IF N_PARAMS(0) LT 3 THEN MAG = 1. ;Mag specified?
  119.     ENDIF
  120. ;
  121.     IF KEYWORD_SET(PIVOT) THEN BEGIN
  122.         XC = X0
  123.         YC = Y0
  124.     END ELSE BEGIN
  125.         xc = (b[1]-1)/2.        ;center of output image.
  126.         yc = (b[2]-1)/2.
  127.     ENDELSE
  128.     theta = -angle/!radeg        ;angle in degrees CLOCKWISE.
  129.     c = cos(theta)*mag        ;cos theta * mag
  130.     s = sin(theta)*mag
  131. ;
  132.     kx = -xc+c*x0-s*y0        ;useful constants.
  133.     ky = -yc+s*x0+c*y0
  134.     kk = 1./(1.+s^2/c^2)
  135. ;
  136.     cx = kk* [s/c^2*ky+kx/c,s/c^2,1/c,0.] ;x coeff...
  137.     cy = kk * [-s/c^2*kx+ky/c,1/c,-s/c^2,0.] ;y coeff.
  138.  
  139.     i = 0                ;assume no interpolation
  140.     if keyword_set(interp) then i=1 ;Bilinear
  141.     if n_elements(cubic) eq 0 then cubic = 0
  142.  
  143.     if n_elements(missing) eq 0 then $
  144.         return,poly_2d(a,cx,cy, i, CUBIC=cubic) $
  145.     else return, poly_2d(a,cx,cy, i, MISSING = missing, CUBIC=cubic)
  146. END
  147.